package com.example.restatr.Atcivity.Traffic;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;

import org.json.JSONException;
import org.json.JSONObject;

public class BingDingPatenteAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private EditText licenseNo;
    private EditText licenseLevel;
    private EditText idCard;
    private EditText auditOffice;
    private TextView applyDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bing_ding_patente_at);

        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        licenseNo = findViewById(R.id.licenseNo);
        licenseLevel = findViewById(R.id.licenseLevel);
        idCard = findViewById(R.id.idCard);
        auditOffice = findViewById(R.id.auditOffice);
        applyDate = findViewById(R.id.applyDate);

        Tools.SetBarTitle(tb, barTitle, "绑定驾驶证", search, this, false);
    }

    //绑定驾驶证
    private void bingDingLicense() {
        new Thread(() -> {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("licenseNo", licenseNo.getText().toString());
                jsonObject.put("licenseLevel", licenseLevel.getText().toString());
                jsonObject.put("idCard", idCard.getText().toString());
                jsonObject.put("auditOffice", auditOffice.getText().toString());
                jsonObject.put("applyDate", applyDate.getText().toString());
                jsonObject.put("verifyDate", "10年");
                String token = Tools.Post("/prod-api/api/traffic/license", MainActivity.sp.getString("token", ""), jsonObject.toString());
                JSONObject jx = new JSONObject(token);
                String msg = jx.getString("msg");
                int code = jx.getInt("code");
                runOnUiThread(() -> {
                    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
                    if (code == 200) {
                        finish();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mileageCommit:
                bingDingLicense();
                break;
            case R.id.applyDate:
                DatePickerDialog datePickerDialog = new DatePickerDialog(this);
                datePickerDialog.create();
                datePickerDialog.show();
                datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        month+=1;
                        applyDate.setText(year + "-" + month + "-" + dayOfMonth);
                        datePickerDialog.dismiss();
                    }
                });
                break;
        }
    }
}